﻿#include "../MyUtils/tools.hpp"
#include "../MyUtils/Timer/ProcessTimer.hpp"
#include <gtest/gtest.h>
#include <string>
#include <iostream>
#include <cstdio>

using namespace std;

using namespace MyUtils;

TEST(Tools, cpp_type_name)
{
    EXPECT_EQ(Tools::cpp_type_name<int>(), string("int"));
    EXPECT_EQ(Tools::cpp_type_name<float>(), string("float"));
    EXPECT_EQ(Tools::cpp_type_name<double>(), string("double"));

    return;
}

TEST(Tools, s_sleep)
{
    MyTimer::ProcessTimer timer;
    Tools::s_sleep(1 * 1000);
    EXPECT_EQ(timer.duration() / 1000, 1);
    return;
}

TEST(Tools, string_format)
{
    std::string str = Tools::string_format("%s %d %f", "hello", 1, 2.0);
    char buf[1024];

    sprintf(buf, "%s %d %f", "hello", 1, 2.0);

    EXPECT_EQ(str, string(buf));
}

TEST(Tools, hex2dec)
{
    const int n = Tools::hex2dec<int>(string("0x1234").substr(2));
    EXPECT_EQ(n, 0x1234);


    const long long ll = Tools::hex2dec<long long>(string("0xf234567890abcdef").substr(2));
    EXPECT_EQ(ll, 0xf234567890abcdefLL);

    const unsigned long long ull = Tools::hex2dec<unsigned long long>(string("0xff34567890abcdef").substr(2));
    EXPECT_EQ(ull, (0xff34567890abcdefULL));
}

TEST(Tools, print_hex)
{
    int a = 0x12345678;
    Tools::print_hex(&a, sizeof(a));
    return;
}

TEST(Tools, getProcessNameAndId)
{
    auto [name,id] = Tools::getProcessNameAndId();
    std::cout << name << "\t" << id << std::endl;
    return;
}

TEST(Tools, localTime)
{
    const string s = Tools::localTime();
    printf("%s\n", s.c_str());
    return;
}

TEST(Tools, replaceSrc)
{
    string s = "abcdefg";
    Tools::replaceSrc("def", "123", s);
    EXPECT_EQ(s, "abc123g");


    s = ".a.b.c.d.";
    Tools::replaceSrc(".", "", s);
    EXPECT_EQ(s, "abcd");
    return;
}

TEST(Tools, replace)
{
    string s = "abcdefg";
    auto d = Tools::replace("def", "123", s);
    EXPECT_EQ(s, "abcdefg");
    EXPECT_EQ(d, "abc123g");
    return;
}

